package cn.shujuhai.common.controller.common;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.shujuhai.common.pluging.data.ResultCode;
import cn.shujuhai.common.pluging.data.ResultVo;
import cn.shujuhai.common.service.AccessLogsService;
import cn.shujuhai.common.service.PVUVService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * AccessLogsController类用于处理与访问日志相关的HTTP请求。
 * 它主要提供两个功能：根据IP地址获取访问次数和获取总的访问次数。
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/visitCount")
public class AccessLogsController {
    @Autowired
    private PVUVService pvuvService;
    // 注入AccessLogsService服务，用于处理访问日志的业务逻辑
    private final AccessLogsService accessLogsService;

    /**
     * 获取总的访问次数。
     *
     * @return 包含总访问次数的ResultVo对象。
     */
    @SaCheckLogin
    @GetMapping("/total-visit-count")
    public ResultVo<Integer> getTotalVisitCount() {
        int totalVisitCount = accessLogsService.getTotalVisitCount();
        return new ResultVo<>(ResultCode.SUCCESS, totalVisitCount);
    }

    @GetMapping("/pv")
    public Long getPV() {
        pvuvService.increasePV();
        return pvuvService.getPV();
    }

    @GetMapping("/uv")
    public Long getUV(String visitorId) {
        pvuvService.increaseUV(visitorId);
        return pvuvService.getUV();
    }
}
